package com.jay.media.kit

import android.app.Activity
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.menu.ActionMenuItemView
import androidx.appcompat.widget.ActionMenuView
import androidx.appcompat.widget.Toolbar

/**
 * @author xuzhou4
 * @date 2021/11/17
 */
object ToolbarColorHelper {

    fun colorToolbar(
        toolbar: Toolbar?,
        color: Int,
        activity: AppCompatActivity
    ) {
        toolbar?.also {
            val porterDuffColorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY)
            toolbar.navigationIcon?.colorFilter = porterDuffColorFilter

            for (i2 in 0 until toolbar.childCount) {
                val childAt: View = toolbar.getChildAt(i2)
                if (childAt is ImageButton) {
                    childAt.drawable.colorFilter = porterDuffColorFilter
                }
                if (childAt is ActionMenuView) {
                    for (i3 in 0 until childAt.childCount) {
                        val childAt2: View = childAt.getChildAt(i3)
                        if (childAt2 is ActionMenuItemView) {
                            for (i4 in childAt2.compoundDrawables.indices) {
                                if (childAt2.compoundDrawables[i4] != null) {
                                    childAt2.post(
                                        ColorFilterRunnable(
                                            childAt2,
                                            i4,
                                            porterDuffColorFilter
                                        )
                                    )
                                }
                            }
                        }
                    }
                }
                setOverflowButtonColor(activity, porterDuffColorFilter)
            }
        }
    }


    private fun setOverflowButtonColor(
        activity: Activity,
        porterDuffColorFilter: PorterDuffColorFilter
    ) {
        val string = activity.getString(R.string.abc_action_menu_overflow_description)
        val viewGroup = activity.window.decorView as ViewGroup
        val arrayList: ArrayList<View> = ArrayList()
        viewGroup.findViewsWithText(
            arrayList,
            string,
            View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION
        )
        if (arrayList.isNotEmpty()) {
            (arrayList[0].parent as ActionMenuView).overflowIcon!!.colorFilter =
                porterDuffColorFilter
        }
    }

    class ColorFilterRunnable internal constructor(
        private val view: View,
        private val drawableIndex: Int,
        private val porterDuffColorFilter: PorterDuffColorFilter
    ) :
        Runnable {
        override fun run() {
            (view as ActionMenuItemView).compoundDrawables[drawableIndex].colorFilter =
                porterDuffColorFilter
        }
    }
}